<template>
	<div class="system-role-container">
		<el-card shadow="hover">
			<div class="system-user-search mb15">
				<el-form :inline="true" :model="tableData.param" ref="queryRef">
					<el-form-item label="角色名称" prop="roleName">
						<el-input size="default" v-model="tableData.param.roleName" placeholder="请输入角色名称" clearable />
					</el-form-item>
					<el-form-item label="状态" prop="status">
						<el-select size="default" placeholder="请选择角色状态" v-model="tableData.param.status" clearable>
							<el-option label="停用" :value="1" />
							<el-option label="正常" :value="0" />
						</el-select>
					</el-form-item>
					<el-form-item>
						<el-button size="default" type="primary" class="ml10" @click="roleList">
							<el-icon>
								<Search />
							</el-icon>
							查询
						</el-button>
						<el-button size="default" @click="resetQuery(queryRef)">
							<el-icon>
								<Refresh />
							</el-icon>
							重置
						</el-button>
						<el-button v-if="permissionStore.isPermissionsBtn('001002001')" size="default" type="success"
							class="ml10" @click="onOpenAddRole">
							<el-icon>
								<FolderAdd />
							</el-icon>
							新增角色
						</el-button>
					</el-form-item>
				</el-form>
			</div>
			<el-table :data="tableData.data" style="width: 100%">
				<el-table-column type="index" label="序号" width="60" />
				<el-table-column prop="roleName" label="角色名称" align="center" show-overflow-tooltip></el-table-column>
				<el-table-column prop="roleSort" label="显示顺序" align="center" show-overflow-tooltip></el-table-column>
				<el-table-column prop="status" label="角色状态" align="center" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.status === 0">正常</el-tag>
						<el-tag type="info" v-else>停用</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="createBy" label="创建人员" align="center" show-overflow-tooltip></el-table-column>
				<el-table-column prop="createTimeTxt" label="创建时间" show-overflow-tooltip></el-table-column>
				<el-table-column prop="updateBy" label="更新人员" align="center" show-overflow-tooltip></el-table-column>
				<el-table-column prop="updateTimeTxt" label="更新时间" show-overflow-tooltip></el-table-column>
				<el-table-column prop="remark" label="备注" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作"
					v-if="permissionStore.isPermissionsBtn(['001002002','001002003','001002004'])"
					width="250" align="center">
					<template #default="scope">
						<el-button size="small" v-if="permissionStore.isPermissionsBtn('001002002')" text type="warning"
							@click="onOpenEditRole(scope.row)"><el-icon>
								<EditPen />
							</el-icon>修改</el-button>
						<el-button size="small" v-if="permissionStore.isPermissionsBtn('001002003')" text type="primary"
							@click="onOpenEditRoleAuth(scope.row)"><el-icon>
								<EditPen />
							</el-icon>数据权限</el-button>
						<el-button v-if="permissionStore.isPermissionsBtn('001002004')" size="small" text type="danger"
							@click="onRowDel(scope.row)"><el-icon><DeleteFilled /></el-icon>删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<pagination v-show="tableData.total > 0" :total="tableData.total" v-model:page="tableData.param.pageNumber"
				v-model:limit="tableData.param.pageLength" @pagination="roleList" />
		</el-card>
		<EditRole ref="editRoleRef" :acType="acType"  @getRoleList="roleList" />
	</div>
</template>

<script lang="ts">
import { toRefs, reactive, onMounted, ref, defineComponent, toRaw, getCurrentInstance } from 'vue';
import { ElMessageBox, ElMessage, FormInstance } from 'element-plus';
import EditRole from './component/editRole.vue';
import { getRoleListApi,delRoleApi } from "@/api/auth/role";
import { formatDateTime } from '@/utils'
import { usePermissionStore } from '@/store/modules/permission'
// 定义接口来定义对象的类型
interface TableData {
	id: number;
	status: number;
	listOrder: number;
	name: string;
	remark: string;
	dataScope: number;
	createdAt: string;
}
interface TableDataState {
	tableData: {
		data: Array<TableData>;
		total: number;
		loading: boolean;
		param: {
			roleName: string;
			status: number;
			pageNumber: number;
			pageLength: number;
		};
	};
}

export default defineComponent({
	name: 'roleList',
	components: { EditRole },
	setup() {
		const addRoleRef = ref();
		const editRoleRef = ref();
		const dataScopeRef = ref();
		const queryRef = ref();
		const acType = ref('')
		const menuData=ref([])
		const deptData=ref([])
		const permissionStore = usePermissionStore()
		const state = reactive<TableDataState>({
			tableData: {
				data: [],
				total: 0,
				loading: false,
				param: {
					roleName: '',
					status: null,
					pageNumber: 1,
					pageLength: 10,
				},
			},
		});
		// 初始化表格数据
		const initTableData = () => {
			roleList()
		};
		const roleList = () => {
			// const data: Array<TableData> = [];
			getRoleListApi(state.tableData.param).then((res: any) => {
				const list = res.data.list ?? []
				state.tableData.data = list.map(item => {
					let obj = {
						...item,
						createTimeTxt: formatDateTime(item.createTime),
						updateTimeTxt: formatDateTime(item.updateTime),
					}
					return obj
				})
				state.tableData.total = res.data.total;
			})
		};
		// 打开新增角色弹窗
		const onOpenAddRole = () => {
			acType.value = 'add'
			editRoleRef.value.openDialog(acType.value);
		};
		// 打开修改角色弹窗
		const onOpenEditRole = (row: Object) => {
			acType.value = 'edit'
			editRoleRef.value.openDialog(acType.value,toRaw(row));
		};
		// 打开修改角色数据权限弹窗
		const onOpenEditRoleAuth = (row: Object) => {
			acType.value = 'auth'
			editRoleRef.value.openDialog(acType.value,toRaw(row));
		};

		/** 重置按钮操作 */
		const resetQuery = (formEl: FormInstance | undefined) => {
			if (!formEl) return
			formEl.resetFields()
			roleList()
		};
		
		// 删除当前行
		const onRowDel = (row) => {
			ElMessageBox.confirm(`此操作将永久删除角色：${row.roleName}, 是否继续?`, '提示', {
				confirmButtonText: '删除',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(() => {
					delRoleApi({id:row.roleId}).then(() => {
						ElMessage.success('删除成功');
						roleList();
					})
				})
				.catch(() => { });
		};

		// 页面加载时
		onMounted(() => {
			initTableData();
		});
		return {
			acType,
			queryRef,
			addRoleRef,
			editRoleRef,
			dataScopeRef,
			permissionStore,
			menuData,
			deptData,
			onOpenAddRole,
			onOpenEditRole,
			onOpenEditRoleAuth,
			onRowDel,
			roleList,
			resetQuery,
			...toRefs(state),
		};
	},
});
</script>
